<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 fdm=marker: */

/**
 * LightBot_Logger_File
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category  LightBot
 * @package   LightBot_Logger
 * @copyright Copyright (c) 2008 Olivier Hoareau <hoareau.olivier@gmail.com>
 * @author    Olivier Hoareau <hoareau.olivier@gmail.com>
 * @license   License http://framework.zend.com/license   New BSD License
 * @version   $Id:$
 */

// {{{ LightBot_Logger_File

/**
 * File Logger implementation
 *
 * @category  LightBot
 * @package   LightBot_Logger
 * @copyright Copyright (c) 2008 Olivier Hoareau <hoareau.olivier@gmail.com>
 * @author    Olivier Hoareau <hoareau.olivier@gmail.com>
 * @license   License http://framework.zend.com/license   New BSD License
 */
class LightBot_Logger_File implements LightBot_ILogger
{
    // {{{ properties

    /**
     * File handle
     * @var resource
     */
    private $handle;

    /**
     * Identifier of this logger
     * @var string
     */
    private $id;

    // }}}
    // {{{ __construct()

    /**
     * Constructs a new logger
     *
     * @param string $id the optional identifier of the logger
     */
    public function __construct($id=null)
    {
        $this->handle = null;
        $this->id     = $id===null ? substr(md5(time().microtime()),0,5) : $id;
    }

    // }}}
    // {{{ getId()

    /**
     * Returns the identifier of this logger
     *
     * @return string the id
     */
    public function getId()
    {
        return $this->id;
    }

    // }}}
    // {{{ configure()

    /**
     * Configures the logger
     *
     * @param  array $conf the configuration to apply
     * @return LightBot_ILogger $this
     */
    public function configure($conf)
    {
        $file = @$conf['logger.file.file'];
        if(!$file){
           throw new Exception("No file specified for the file logger (use the configuration key 'logger.file.file')");
        }
        if(!is_dir(dirname($file))){
           throw new Exception("Directory '".dirname($file)."' does not exists are is not readable");
        }
        $this->handle = fopen($file,'a+');
    }

    // }}}
    // {{{ log()

    /**
     * Log the specified debug message
     *
     * @param string $msg the message
     * @return LightBot_ILogger $this
     */
    public function log($msg)
    {
        return $this->debug($msg);
    }

    // }}}
    // {{{ debug()

    /**
     * Log the specified debug message
     *
     * @param string $msg the message
     * @return LightBot_ILogger $this
     */
    public function debug($msg)
    {
        $this->write("[".date("Y-m-d H:i:s",time())."] [".$this->id."] ["."DEBUG"."] ".$msg);
        return $this;
    }

    // }}}
    // {{{ error()

    /**
     * Log the specified error message
     *
     * @param string $msg the message
     * @return LightBot_ILogger $this
     */
    public function error($msg)
    {
        $this->write("[".date("Y-m-d H:i:s",time())."] [".$this->id."] ["."ERROR"."] ".$msg);
        return $this;
    }

    // }}}
    // {{{ fatal()

    /**
     * Log the specified fatal message
     *
     * @param string $msg the message
     * @return LightBot_ILogger $this
     */
    public function fatal($msg)
    {
        $this->write("[".date("Y-m-d H:i:s",time())."] [".$this->id."] ["."FATAL"."] ".$msg);
        return $this;
    }

    // }}}
    // {{{ info()

    /**
     * Log the specified info message
     *
     * @param string $msg the message
     * @return LightBot_ILogger $this
     */
    public function info($msg)
    {
        $this->write("[".date("Y-m-d H:i:s",time())."] [".$this->id."] ["."INFO"."]  ".$msg);
        return $this;
    }

    // }}}
    // {{{ warn()

    /**
     * Log the specified warning message
     *
     * @param string $msg the message
     * @return LightBot_ILogger $this
     */
    public function warn($msg)
    {
        $this->write("[".date("Y-m-d H:i:s",time())."] [".$this->id."] ["."WARN"."]  ".$msg);
        return $this;
    }

    // }}}
    // {{{ write()

    /**
     * Writes the specified message to the log file
     *
     * @param string $msg the message
     */
    protected function write($msg)
    {
        if(is_resource($this->handle)){
            fwrite($this->handle,$msg."\n");
        }
    }

    // }}}
    // {{{ __destruct()

    public function __destruct()
    {
        if(is_resource($this->handle)){
            fclose($this->handle);
        }
    }

    // }}}
}   

// }}}

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * c-hanging-comment-ender-p: nil
 * End:
 */
